//
//  CYbankcarddetailVC.m
//  CreditAddressBook
//
//  Created by Mosu&Cuu on 16/3/30.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "CYbankcarddetailVC.h"
#import "LEBarButtonItem.h"
#import "CYUser.h"
#import "CYLoginHttpTool.h"
#import "CYHttpTool.h"
#import "CYJsonInfo.h"
#import "NSObject+MJKeyValue.h"
#import "CYBankcardBandTableViewController.h"
#import "CYJumpsTool.h"
#import "MBProgressHUD+LE.h"

@interface CYbankcarddetailVC ()
@property (weak, nonatomic) IBOutlet UIView *bankgroundView;
@property (weak, nonatomic) IBOutlet UILabel *banktypeLab;
@property (weak, nonatomic) IBOutlet UILabel *bankcardLab;
@property (weak, nonatomic) IBOutlet UILabel *bankAreaLab;
@property (weak, nonatomic) IBOutlet UILabel *bankAddressLab;

@end

@implementation CYbankcarddetailVC

- (void)viewWillAppear:(BOOL)animated{
    [self setupNavigationView];
    [self getbankcardinfo];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的银行卡";
//    _bankgroundView.layer.cornerRadius = 8.0f;
//    _bankgroundView.layer.borderWidth = 1;
    _bankgroundView.layer.borderColor = CYColor(102, 207, 116, 1).CGColor;
    // Do any additional setup after loading the view from its nib.
}

-(void)getbankcardinfo{
    CYUser *user = [CYLoginHttpTool userData].user;
      [MBProgressHUD showMessage:nil toView:self.view];
    [CYHttpTool postWithURL:[CYURL shared].getBandCardStatus params:@{@"userId":user.userId,@"token":user.token} success:^(id json) {
         [MBProgressHUD hideHUDForView:self.view animated:YES];
        CYJsonInfo *jsoninfo = [CYJsonInfo objectWithKeyValues:json];
        if (jsoninfo.isSuccess) {
             [MBProgressHUD hideHUDForView:self.view animated:YES];
            NSDictionary *data = jsoninfo.data;
            NSString *cardNo = [data valueForKey:@"cardNo"];
            NSString *cardType = [data valueForKey:@"cardType"];
            NSString *city = [data valueForKey:@"city"];
            NSString *saleNetwork = [data valueForKey:@"saleNetwork"];
            NSString *province = [data valueForKey:@"province"];
             if (cardNo.length >15) {
                  _bankcardLab.text = [NSString stringWithFormat:@"%@****%@", [cardNo substringToIndex:4], [cardNo substringFromIndex:12]];
             }else {
                  _bankcardLab.text = cardNo;
                  
             }
            _banktypeLab.text = cardType;
            _bankAreaLab.text = [NSString stringWithFormat:@"开户地区:%@-%@",province,city];
            _bankAddressLab.text =[NSString stringWithFormat:@"开户银行:%@" ,saleNetwork];
            
        }
    } failure:^(NSError *error) {
         CYLog(@"error = %@",error);
         [MBProgressHUD hideHUDForView:self.view animated:YES];

        
    }];
}

- (IBAction)changeBankcardBtn:(id)sender {
    
    CYBankcardBandTableViewController  *bacd = [CYBankcardBandTableViewController le_loadFromStoryBoard:[CYJumpsTool mainSB]];
    bacd.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:bacd animated:YES];
    
    
}

- (void)setupNavigationView{
    
    UIView *naviView = [[UIView alloc] init];
    naviView.backgroundColor = CYNaviThemeColor;
    naviView.frame = CGRectMake(0, 0, self.view.width, 64);
    [self.view addSubview:naviView];
}

#pragma mark - 通知
- (void)setupNoti {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cardbandclick)name:CYCardBandNotification object:nil];
}

-(void)cardbandclick{
    CYUser *user = [CYLoginHttpTool userData].user;
    [CYHttpTool postWithURL:[CYURL shared].getBandCardStatus params:@{@"userId":user.userId,@"token":user.token} success:^(id json) {
        CYJsonInfo *jsoninfo = [CYJsonInfo objectWithKeyValues:json];
        if (jsoninfo.isSuccess) {
            NSDictionary *data = jsoninfo.data;
            NSString *cardNo = [data valueForKey:@"cardNo"];
            NSString *cardType = [data valueForKey:@"cardType"];
            NSString *city = [data valueForKey:@"city"];
            NSString *saleNetwork = [data valueForKey:@"saleNetwork"];
            NSString *province = [data valueForKey:@"province"];
            _bankcardLab.text = [NSString stringWithFormat:@"%@****%@", [cardNo substringToIndex:4], [cardNo substringFromIndex:15]];
            _banktypeLab.text = cardType;
            _bankAreaLab.text = [NSString stringWithFormat:@"开户地区:%@-%@",province,city];
            _bankAddressLab.text =[NSString stringWithFormat:@"开户银行:%@" ,saleNetwork];
            
        }
    } failure:^(NSError *error) {
        CYLog(@"error = %@",error);
        
    }];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
